*************************
*** BETA COEFFICIENTS ***
*************************

	*** Load data
		use "$output/combined_data.dta", clear
		capture drop _merge
		drop *_pop
		drop if year < 1960
		reshape wide $datasets, i(ccode country) j(year)
		*drop if pwt1990==.|wdi1990==.|mad1990==.
		
	*** Cycle through regressions
		local j = 1	
		foreach data in $datasets {			
			// Customize window by dataset
			local firstyear = 1960
			if "`data'" == "pwt" | "`data'" == "wdi" {
				local lastyear = 2019
			}
			if "`data'" == "mad" {
				local lastyear = 2018
			}	
			local endyear = `lastyear' - 1 		
				forval startyear = `firstyear'(1)`endyear' {
					local startplus1 = `startyear' + 1
					forval outcomeyear = `startplus1'(1)`lastyear' {
						gen outcome = (log(`data'`outcomeyear'/`data'`startyear')/(`outcomeyear' - `startyear'))
						gen initial = log(`data'`startyear')
						qui nl (outcome = {b0=1} - (1 - exp(-1*{b1=0.00}*(`outcomeyear' - `startyear')))/(`outcomeyear' - `startyear')*initial), vce(robust)  
						preserve
							clear
							set obs 1
							tempfile file`j'
							gen measure = "`data'"
							gen beta = _b[/b1]
							gen se = _se[/b1]
							gen lower = _b[/b1] - invttail(`e(df_r)',0.025)*_se[/b1]
							gen upper = _b[/b1] + invttail(`e(df_r)',0.025)*_se[/b1]
							gen tstat = _b[/b1]/_se[/b1]
							gen pval =2*ttail(`e(df_r)',abs(tstat))
							gen n = `e(N)'
							gen startyear = `startyear'
							gen endyear = `outcomeyear'
							save `file`j''
						restore
						drop outcome initial
						local ++ j
					}
				}
		}
	
	*** Combine results
		clear
		local jminus1 = `j' - 1
		forval i = 1/`jminus1' {
			append using `file`i''
		}
		gen period = endyear - startyear
		bys measure: egen maxend = max(endyear)
		save "$output/coefficients.dta", replace


***********************	
*** CONSTANT SAMPLE ***
***********************
		
	*** Load data
		use "$output/combined_data.dta", clear
		capture drop _merge
		drop *_pop
		drop if year < 1980
		foreach data in $datasets {
			bys ccode: egen `data'count = count(`data')
		}
		replace pwt = . if pwtcount != 40
		replace mad = . if madcount != 39
		replace wdi = . if wdicount != 40 
		reshape wide $datasets, i(ccode country) j(year)
						
	*** Cycle through regressions
		local j = 1	
		foreach data in $datasets {			
			// Customize window by dataset
			local firstyear = 1980
			if "`data'" == "pwt" | "`data'" == "wdi" {
				local lastyear = 2019
			}
			if "`data'" == "mad" {
				local lastyear = 2018
			}	
			local endyear = `lastyear' - 1 		
				forval startyear = `firstyear'(1)`endyear' {
					local startplus1 = `startyear' + 1
					forval outcomeyear = `startplus1'(1)`lastyear' {
						gen outcome = (log(`data'`outcomeyear'/`data'`startyear')/(`outcomeyear' - `startyear'))
						gen initial = log(`data'`startyear')
						qui nl (outcome = {b0=1} - (1 - exp(-1*{b1=0.00}*(`outcomeyear' - `startyear')))/(`outcomeyear' - `startyear')*initial), vce(robust)  
						preserve
							clear
							set obs 1
							tempfile file`j'
							gen measure = "`data'"
							gen beta = _b[/b1]
							gen se = _se[/b1]
							gen lower = _b[/b1] - invttail(`e(df_r)',0.025)*_se[/b1]
							gen upper = _b[/b1] + invttail(`e(df_r)',0.025)*_se[/b1]
							gen tstat = _b[/b1]/_se[/b1]
							gen pval =2*ttail(`e(df_r)',abs(tstat))
							gen n = `e(N)'
							gen startyear = `startyear'
							gen endyear = `outcomeyear'
							save `file`j''
						restore
						drop outcome initial
						local ++ j
					}
				}
		}
	
	*** Combine results
		clear
		local jminus1 = `j' - 1
		forval i = 1/`jminus1' {
			append using `file`i''
		}
		gen period = endyear - startyear
		bys measure: egen maxend = max(endyear)
		save "$output/coefficients_constsamp.dta", replace
		

*************************
*** SIGMA CONVERGENCE ***
*************************

	*** Load data
		use "$output/combined_data.dta", clear
		capture drop _merge
		drop *_pop
		reshape wide $datasets, i(ccode country) j(year)
		
	*** Cycle through regressions
		local j = 1	
		foreach data in $datasets {			
			// Customize window by dataset
			local firstyear = 1960
			if "`data'" == "pwt" | "`data'" == "wdi" {
				local lastyear = 2019
			}
			if "`data'" == "mad" {
				local lastyear = 2018
			}	
			forval startyear = `firstyear'(1)`lastyear' {
				gen logy = log(`data'`startyear')
				ci variances logy
						preserve
							clear
							set obs 1
							tempfile file`j'
							gen measure = "`data'"
							gen var =  r(Var) 
							gen var_l = `r(lb)'
							gen var_u = `r(ub)'
							gen n = `e(N)'
							gen year = `startyear'
							gen fixed = 0
							save `file`j''
						restore
						drop logy
						local ++ j
					}
		}
	
	*** Fixed 1980 sample
		use "$output/combined_data.dta", clear
		capture drop _merge
		drop *_pop
		drop if year < 1980
		foreach data in $datasets {
			bys ccode: egen `data'count = count(`data')
		}
		replace pwt = . if pwtcount != 40
		replace mad = . if madcount != 39
		replace wdi = . if wdicount != 40 
		reshape wide $datasets, i(ccode country) j(year)

		foreach data in $datasets {			
			// Customize window by dataset
			local firstyear = 1980
			if "`data'" == "pwt" | "`data'" == "wdi" {
				local lastyear = 2019
			}
			if "`data'" == "mad" {
				local lastyear = 2018
			}	
			forval startyear = `firstyear'(1)`lastyear' {
				gen logy = log(`data'`startyear')
				ci variances logy 
						preserve
							clear
							set obs 1
							tempfile file`j'
							gen measure = "`data'"
							gen var =  r(Var) 
							gen var_l = `r(lb)'
							gen var_u = `r(ub)'
							gen n = `e(N)'
							gen year = `startyear'
							gen fixed = 1
							save `file`j''
						restore
						drop logy
						local ++ j
					}
		}
		
	*** Combine results
		clear
		local jminus1 = `j' - 1
		forval i = 1/`jminus1' {
			append using `file`i''
		}
		save "$output/sigma_convergence.dta", replace
		
*********************
*** MIDDLE INCOME ***
*********************

	*** Cycle through regressions
		local j = 1	
		foreach data in $datasets {			
			// Customize window by dataset
			local firstyear = 1960
			if "`data'" == "pwt" | "`data'" == "wdi" {
				local lastyear = 2019
			}
			if "`data'" == "mad" {
				local lastyear = 2018
			}	
			local endyear = `lastyear' - 1
			*** Load data
				use "$output/combined_data.dta", clear
					
			*** Define income groups
				summ `data' if ccode == "USA" & year == 1960 
				local usa = `r(mean)'
				summ pwt if year == 1960, det 
				local lowthresh = `r(p25)'/`usa'
				local highthresh = `r(p75)'/`usa'
				gen us = `data' if ccode == "USA"
				sort year us
				by year: replace us = us[_n-1] if missing(us) & !missing(us[_n-1])
				gen catl = `lowthresh'*us
				gen catu = `highthresh'*us
				gen cat = "H" if `data' >= catu & !missing(`data')
				replace cat = "M" if `data' < catu & `data' >=catl & !missing(`data')
				replace cat = "L" if `data' < catl 
				gen middle = cat == "M"
				keep $datasets middle ccode country year 
				reshape wide $datasets middle, i(ccode country) j(year)
					forval startyear = `firstyear'(1)`endyear' {
						local outcomeyear = `lastyear' 
						gen outcome = (log(`data'`outcomeyear'/`data'`startyear')/(`outcomeyear' - `startyear'))*100
						gen initial = log(`data'`startyear')
						qui reg outcome initial middle`startyear', robust  
						preserve
							clear
							set obs 1
							tempfile file`j'
							gen measure = "`data'"
							gen beta = _b[initial]
							gen se = _se[initial]
							gen lower = _b[initial] - invttail(`e(df_r)',0.025)*_se[initial]
							gen upper = _b[initial] + invttail(`e(df_r)',0.025)*_se[initial]
							gen tstat = _b[initial]/_se[initial]
							gen pval =2*ttail(`e(df_r)',abs(tstat))
							gen mbeta = _b[middle`startyear']
							gen mse = _se[middle`startyear']
							gen mlower = _b[middle`startyear'] - invttail(`e(df_r)',0.025)*_se[middle`startyear']
							gen mupper = _b[middle`startyear'] + invttail(`e(df_r)',0.025)*_se[middle`startyear']
							gen mtstat = _b[middle`startyear']/_se[middle`startyear']
							gen mpval =2*ttail(`e(df_r)',abs(mtstat))
							gen n = `e(N)'
							gen startyear = `startyear'
							gen endyear = `outcomeyear'
							save `file`j''
						restore
						drop outcome initial
						local ++ j
					}
		}
	
	*** Combine results
		clear
		local jminus1 = `j' - 1
		forval i = 1/`jminus1' {
			append using `file`i''
		}
		gen period = endyear - startyear
		bys measure: egen maxend = max(endyear)
		save "$output/mit_coefficients.dta", replace		